
import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/framework/bean/x_bean_data_table.dart';
import 'package:xsdk_flutter/xsdk/framework/page/x_page_state.dart';
import 'package:xsdk_flutter/xsdk/utils/data_table/x_utils_data_table.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';


/*
页面父类基础界面
DataTable通用State

https://blog.csdn.net/mengks1987/article/details/104661591
https://www.jianshu.com/p/23fc6f3acd7c

第三方库:
  https://pub.flutter-io.cn/packages/syncfusion_flutter_datagrid
  https://pub-web.flutter-io.cn/packages/data_table_2
  https://pub-web.flutter-io.cn/packages/pluto_grid
  https://weblaze.dev/pluto_grid/build/web/#feature/column-freezing

 */
abstract class XPageDataTableState extends XPageState {

  //子类取消required,(这里强制required则子类会提示生成构造方法)
  XPageDataTableState({
    super.isKeepPageState=false,
    super.xTag,
  });

  /*
  构建datatable的行数据
   */
  @required
  DataRow getDataTableDataRow(dynamic item);

  @required
  List<XBeanDataTableLabel> getDataTableColumnsLabels();


  //重写方法///////////////////////////////////////////////////

  @override
  void onCreate() {
    super.onCreate();
    if(widget.dataTableColumnsLabels.isEmpty){
      widget.dataTableColumnsLabels.addAll(getDataTableColumnsLabels());
    }
  }


  EdgeInsets getListViewPadding(){
    return XWidgetUtils.getEdgeInsets(0, 0, 0, 0);
  }

  //当没有数据时,显示提示图
  Widget? getEmptyDataWidget(){
    return null;
  }

  Widget? getRoundedRectWidgetBackgroundInnerHeader(){
    return null;
  }

  List<Widget>? getListViewHeaderWidgets(){
    return null;
  }

  List<Widget>? getListViewFooterWidgets(){
    return null;
  }


  //背景圆角Widget(这个listview在一个圆角Widget中)
  bool getIsUseRoundedRectWidgetBackground(){
    return false;
  }

  //是否启用分页加载数据
  bool getIsEnablePageLoadData(){
    return false;
  }

  //是否启用下拉刷新数据
  bool getIsEnableRefreshLoadData(){
    return false;
  }

  @override
  bool getIsPageIndexStartWith1() {
    //分页加载是否从第1页开始,否则从0开始
    return true;
  }

  //弹出输入框重绘高度
  @override
  bool getIsResizeToAvoidBottomInset() {
    return true;
  }

  Color getDataTableHeadingRowColor(){
    return Colors.white;
  }


  //私有方法/////////////////////////////////////////////////////////

  Widget _createDataTableView(){

    List<Widget> listWidgets=[];
    List<Widget>? listViewHeaderWidgets=getListViewHeaderWidgets();
    List<Widget>? listViewFooterWidgets=getListViewFooterWidgets();
    //
    if(listViewHeaderWidgets!=null && listViewHeaderWidgets.isNotEmpty){
      listWidgets.addAll(listViewHeaderWidgets);
    }


    //数据表格DataTable(支持横向滑动)
    listWidgets.add(
        XWidgetUtils.getLayoutSingleChildScrollView(
          scrollDirection:Axis.horizontal,
          child: XDataTableUtils.getWidgetDataTable(
            headingRowColor: getDataTableHeadingRowColor(),
            columnsLabels: widget.dataTableColumnsLabels,
            listData: widget.listData,
            rowItemBuildFunction: (dynamic item) {
              return getDataTableDataRow(item);
            },
          ),
        )
    );
    //
    if(listViewFooterWidgets!=null&&listViewFooterWidgets.isNotEmpty){
      listWidgets.addAll(listViewFooterWidgets);
    }

    return XWidgetUtils.getLayoutSingleChildScrollView(
      padding:getListViewPadding(),
      //支持分页
      initialScrollOffset: widget.currentListViewPosition,
      onNotifyScrollStop: !getIsEnablePageLoadData()?null:(ScrollController? scrollController){
        //启用分页加载数据
        widget.currentListViewPosition=scrollController?.offset??0.0;
      },
      onNotifyScrollBottomPosition: !getIsEnablePageLoadData()?null:(e){
        //滑动到底部通知,加载下一页
        if(widget.currentPageIndex<widget.totalPages){
          updateData(pageIndex: widget.currentPageIndex+1);
        }
      },
      onRefresh: !getIsEnableRefreshLoadData()?null:() async{
        widget.currentListViewPosition=0.0;
        updateData(pageIndex: getIsPageIndexStartWith1()?1:0);
      },
      child: XWidgetUtils.getLayoutColumn(children: listWidgets),
    );

  }

  /*
  添加AnimatedList控件
   */
  @override
  Widget getBodyWidget(BuildContext context) {

    if(widget.listData.length==0 && getEmptyDataWidget()!=null){
      return getEmptyDataWidget()!;
    }

    if(getIsUseRoundedRectWidgetBackground()){
      //实测:圆角layout会自动暂满剩余空间,注意:这里不能在Column嵌入listview和Spacer(除非固定listview高度)
      Widget? headerWidget=getRoundedRectWidgetBackgroundInnerHeader();
      if(headerWidget==null){
        return XWidgetUtils.getXLayoutRoundedRect(
            margin: XWidgetUtils.getEdgeInsets(10, 10, 10, 10),
            child:_createDataTableView()
        );
      }else{
        return XWidgetUtils.getXLayoutRoundedRect(
            margin: XWidgetUtils.getEdgeInsets(10, 10, 10, 10),
            child:XWidgetUtils.getLayoutColumn(
                children: [
                  headerWidget,
                  XWidgetUtils.getLayoutExpanded(_createDataTableView())
                ]
            )
        );
      }

    }else{
      return _createDataTableView();
    }

  }



  //手动点击按钮添加,替换,删除(按位置删除,适合存在重复item的情况,适合ListView,AnimatedList使用,但不适合Dismissible使用),/////////////////////////////////////////////////////////
  void setXListData_pageIndexData(int? pageIndex, int? pageCount, List<dynamic>? items){
    if(mounted){
      setState((){

        pageIndex??=0;
        pageCount??=0;

        //判断是否删除所有数据
        if(getIsPageIndexStartWith1()){
          //分页从第1页开始
          if(pageIndex==0 || pageIndex==1){
            widget.listData.clear();
          }
        }else{
          //分页从0开始
          if(pageIndex==0){
            widget.listData.clear();
          }
        }

        widget.currentPageIndex=pageIndex!;
        widget.totalPages=pageCount!;

        //
        if(items!=null){
          widget.listData.addAll(items);
        }

      });
    }

  }


  void setXListData_addAllItems(
      List<dynamic>? items,
      {
        int? position, //插入位置
        bool isReplaceAllItems=true,//替换所有数据
      }
      ){

    if(mounted){

      setState((){
        if(isReplaceAllItems){
          //删除所有数据
          widget.listData.clear();
        }

        if(items!=null){
          //先添加item,后更新
          if(position==null || position!<0){
            //在末尾添加
            position=widget.listData.length;
            widget.listData.addAll(items);
          }else{
            //按位置插入
            widget.listData.insertAll(position!, items);
          }
        }

      });

    }

  }


}